# encoding:utf-8
'''''
Created on 2015年9月6日

@author: ZHOUMEIXU204
朴素贝叶斯实现过程
'''

# 在该算法中类标签为1和0，如果是多标签稍微改动代码既可
import numpy as np




def loadDataSet():
    postingList = [['my', 'dog', 'has', 'flea', 'problems', 'help', 'please'], \
                   ['maybe', 'not', 'take', 'him', 'to', 'dog', 'park', 'stupid'], \
                   ['my', 'dalmation', 'is', 'so', 'cute', 'I', 'love', 'him'], \
                   ['stop', 'posting', 'stupid', 'worthless', 'garbage'], \
                   ['mr', 'licks', 'ate', 'my', 'steak', 'how', 'to', 'stop', 'him'], \
                   ['quit', 'buying', 'worthless', 'dog', 'food', 'stupid']]
    classVec = [0, 1, 0, 1, 0, 1]  # 1 is abusive, 0 not
    return postingList, classVec


def createVocabList(dataset):
    vocabSet = set([])
    for document in dataset:
        vocabSet = vocabSet | set(document)
    return list(vocabSet)


def setOfWordseVec(vocabList, inputSet):
    returnVec = [0] * len(vocabList)
    for word in inputSet:
        if word in vocabList:
            returnVec[vocabList.index(word)] = 1  # vocabList.index()  函数获取vocabList列表某个元素的位置，这段代码得到一个只包含0和1的列表
        else:
            print("the word :%s is not in my  Vocabulary!" % word)
    return returnVec


listOPosts, listClasses = loadDataSet()
myVocabList = createVocabList(listOPosts)
print(len(myVocabList))
print(myVocabList)
print(setOfWordseVec(myVocabList, listOPosts[0]))
print(setOfWordseVec(myVocabList, listOPosts[3]))


# 上述代码是将文本转化为向量的形式，如果出现则在向量中为1，若不出现 ，则为0


def trainNB0(trainMatrix, trainCategory):  # 创建朴素贝叶斯分类器函数
    numTrainDocs = len(trainMatrix)
    numWords = len(trainMatrix[0])
    pAbusive = sum(trainCategory) / float(numTrainDocs)
    p0Num = np.ones(numWords);
    p1Num = np.ones(numWords)
    p0Deom = 2.0;
    p1Deom = 2.0
    for i in range(numTrainDocs):
        if trainCategory[i] == 1:
            p1Num += trainMatrix[i]
            p1Deom += sum(trainMatrix[i])
        else:
            p0Num += trainMatrix[i]
            p0Deom += sum(trainMatrix[i])
    p1vect = np.log(p1Num / p1Deom)  # change  to log
    p0vect = np.log(p0Num / p0Deom)  # change to log
    return p0vect, p1vect, pAbusive


listOPosts, listClasses = loadDataSet()
myVocabList = createVocabList(listOPosts)
trainMat = []
for postinDoc in listOPosts:
    trainMat.append(setOfWordseVec(myVocabList, postinDoc))

p0V, p1V, pAb = trainNB0(trainMat, listClasses)
if __name__ != '__main__':
    print("p0的概况")
    print(p0V)
    print("p1的概率")
    print(p1V)
    print("pAb的概率")
    print(pAb)


# 构建样本分类器testEntry=['love','my','dalmation']  testEntry=['stupid','garbage']到底属于哪个类别
def classifyNB(vec2Classify, p0Vec, p1Vec, pClass1):
    p1 = sum(vec2Classify * p1Vec) + np.log(pClass1)
    p0 = sum(vec2Classify * p0Vec) + np.log(1.0 - pClass1)
    if p1 > p0:
        return 1
    else:
        return 0


def testingNB():
    listOPosts, listClasses = loadDataSet()
    myVocabList = createVocabList(listOPosts)
    trainMat = []
    for postinDoc in listOPosts:
        trainMat.append(setOfWordseVec(myVocabList, postinDoc))
    p0V, p1V, pAb = trainNB0(np.array(trainMat), np.array(listClasses))
    print("p0V={0}".format(p0V))
    print("p1V={0}".format(p1V))
    print("pAb={0}".format(pAb))
    testEntry = ['love', 'my', 'dalmation']
    thisDoc = np.array(setOfWordseVec(myVocabList, testEntry))
    print(thisDoc)
    print("vec2Classify*p0Vec={0}".format(thisDoc * p0V))
    print(testEntry, 'classified as :', classifyNB(thisDoc, p0V, p1V, pAb))
    testEntry = ['stupid', 'garbage']
    thisDoc = np.array(setOfWordseVec(myVocabList, testEntry))
    print(thisDoc)
    print(testEntry, 'classified as :', classifyNB(thisDoc, p0V, p1V, pAb))


if __name__ == '__main__':
    testingNB()